//
//  AdFeeDetailViewController.m
//  share_business
//
//  Created by baidufz on 2017/9/12.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "AdFeeDetailViewController.h"
#import "AdFeeTitleCell.h"
#import "AdFeeMiddleCell.h"
#import "SpaceCell.h"
#import "CommonDetailItemCell.h"
#import "CommonDetailTitleCell.h"
#import "UICollectionView+RegisterCell.h"
#import "ApplyWithdrawViewController.h"

#define MiddleCellWidth  (ScreenWidth - (15 * 3))/2

@interface AdFeeDetailViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation AdFeeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self registerCells];
    [self initAdFeeDetail];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self defaultMessageRightButton];
    self.navigationItem.title = @"广告费详情";

}

- (void)initAdFeeDetail {
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
}
- (void)registerCells {
    [_collectionView registerHeaderCell:[AdFeeTitleCell class]];
    [_collectionView registerCell:[AdFeeMiddleCell class]];
    [_collectionView registerHeaderCell:[CommonDetailTitleCell class]];
    [_collectionView registerFooterCell:[SpaceCell class]];
    [_collectionView registerCell:[CommonDetailItemCell class]];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 1) {
        return 4;
    } else if (section == 2) {
        return 3;
    }
    return 0;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader] && indexPath.section == 0) {
        AdFeeTitleCell *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([AdFeeTitleCell class]) forIndexPath:indexPath];
        
        return header;
    } else if([kind isEqualToString:UICollectionElementKindSectionHeader] && indexPath.section == 1) {
        AdFeeTitleCell *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([AdFeeTitleCell class]) forIndexPath:indexPath];
        // 临时占位，不显示
        return header;
    } else if([kind isEqualToString:UICollectionElementKindSectionHeader] && indexPath.section == 2) {
        CommonDetailTitleCell *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([CommonDetailTitleCell class]) forIndexPath:indexPath];
        
        return header;
    } else if([kind isEqualToString:UICollectionElementKindSectionFooter]){
        SpaceCell *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([SpaceCell class]) forIndexPath:indexPath];
        footer.backgroundColor = [UIColor lightGrayColor];
        return footer;
    }
    return nil;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        AdFeeMiddleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([AdFeeMiddleCell class]) forIndexPath:indexPath];
        
        return cell;
    }else if (indexPath.section == 2) {
        CommonDetailItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CommonDetailItemCell class]) forIndexPath:indexPath];
        
        return cell;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return CGSizeMake(MiddleCellWidth, 75);
    }
    return CGSizeMake(ScreenWidth, 60);
}

/** 底部的尺寸*/
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 2) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(1, 10);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    CGSize size = CGSizeMake(0, 0);
    if (section == 0) {
        size = CGSizeMake(ScreenWidth, 176);
    } else if (section == 1) {
        size = CGSizeMake(ScreenWidth, 0);
    } else if(section == 2) {
        size = CGSizeMake(ScreenWidth, 45);
    }
    return size;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if(section == 1) {
    return UIEdgeInsetsMake(15, 15, 15, 15);//分别为上、左、下、右
    }
    return  UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 1) {
        return 15;
    }
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 1) {
        return 15;
    }

    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"section -- %ld, row -- %ld",(long)indexPath.section,(long)indexPath.row);
}


- (IBAction)applyWithdraw:(id)sender {
    [self.navigationController pushViewController:[ApplyWithdrawViewController new] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
